package com.shujia.flink.core

import java.lang
import java.util.Properties

import org.apache.flink.api.common.serialization.SimpleStringSchema
import org.apache.flink.contrib.streaming.state.RocksDBStateBackend
import org.apache.flink.runtime.state.StateBackend
import org.apache.flink.streaming.api.CheckpointingMode
import org.apache.flink.streaming.api.environment.CheckpointConfig.ExternalizedCheckpointCleanup
import org.apache.flink.streaming.api.scala._
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaProducer.Semantic
import org.apache.flink.streaming.connectors.kafka.{FlinkKafkaConsumer, FlinkKafkaProducer, KafkaSerializationSchema}
import org.apache.flink.streaming.connectors.kafka.partitioner.FlinkFixedPartitioner

object Demo9ExactlyOnce {
  def main(args: Array[String]): Unit = {



    val env: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment


    // 每 1000ms 开始一次 checkpoint
    env.enableCheckpointing(5000)

    // 高级选项：

    // 设置模式为精确一次 (这是默认值)
    env.getCheckpointConfig.setCheckpointingMode(CheckpointingMode.EXACTLY_ONCE)

    // 确认 checkpoints 之间的时间会进行 500 ms
    env.getCheckpointConfig.setMinPauseBetweenCheckpoints(500)

    // Checkpoint 必须在一分钟内完成，否则就会被抛弃
    env.getCheckpointConfig.setCheckpointTimeout(60000)

    // 同一时间只允许一个 checkpoint 进行
    env.getCheckpointConfig.setMaxConcurrentCheckpoints(1)

    // 开启在 job 中止后仍然保留的 externalized checkpoints
    env.getCheckpointConfig.enableExternalizedCheckpoints(ExternalizedCheckpointCleanup.RETAIN_ON_CANCELLATION)


    val stateBackend: StateBackend = new RocksDBStateBackend("hdfs://master:9000/flink/checkpoint", true)

    env.setStateBackend(stateBackend)


    val properties = new Properties()
    properties.setProperty("bootstrap.servers", "master:9092,node1:9092,node2:9092")
    properties.setProperty("group.id", "asdasdsasd")
    properties.setProperty("auto.offset.reset", "earliest")

    val consumer = new FlinkKafkaConsumer[String]("checkpoint", new SimpleStringSchema(), properties)


    val kafkaDS: DataStream[String] = env.addSource(consumer)


    val wordsDS: DataStream[String] = kafkaDS.flatMap(_.split(","))

    /*
        val myProducer = new FlinkKafkaProducer[String](
          "master:9092,node1:9092,node2:9092", // broker 列表
          "words", // 目标 topic
          new SimpleStringSchema) // 序列化 schema*/


    val properties1 = new Properties
    properties1.setProperty("bootstrap.servers", "master:9092,node1:9092,node2:9092")
    //不能大于15分钟
    properties1.setProperty("transaction.timeout.ms", 5 * 60 * 1000 + "")

    val myProducer = new FlinkKafkaProducer[String](
      "words", // 目标 topic
      new SimpleStringSchema,
      properties1,
      null,//分区方法
      Semantic.EXACTLY_ONCE, // 唯一一次
      5
    ) // 序列化 schema

    //将数据发送到kafka 中
    wordsDS.addSink(myProducer)


    env.execute()

    /**
      *
      * 读取已提交的数据，不然也会有重复数据
      *
      * kafka-console-consumer.sh --bootstrap-server  master:9092,node1:9092,node2:9092 --isolation-level read_committed   -
      * -from-beginning --topic words
      *
      */


  }
}

